home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVXXF.C < prev    next >
C/C++ Source or Header  |  1993-05-28  |  14KB  |  466 lines

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevxxf.c */
  20. /* External font (xfont) implementation for X11. */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "x_.h"
  24. #include "gx.h"
  25. #include "gxdevice.h"
  26. #include "gdevx.h"
  27. #include "gsutil.h"
  28. #include "gserrors.h"
  29. #include "gxxfont.h"
  30.  
  31. extern gx_device_X gs_x11_device;
  32.  
  33. extern const byte gs_map_std_to_iso[256];
  34. extern const byte gs_map_iso_to_std[256];
  35.  
  36. /* Declare the xfont procedures */
  37. private xfont_proc_lookup_font(x_lookup_font);
  38. private xfont_proc_char_xglyph(x_char_xglyph);
  39. private xfont_proc_char_metrics(x_char_metrics);
  40. private xfont_proc_render_char(x_render_char);
  41. private xfont_proc_release(x_release);
  42. private gx_xfont_procs x_xfont_procs = {
  43.     x_lookup_font,
  44.     x_char_xglyph,
  45.     x_char_metrics,
  46.     x_render_char,
  47.     x_release
  48. };
  49.  
  50. /* Return the xfont procedure record. */
  51. gx_xfont_procs *
  52. x_get_xfont_procs(gx_device *dev)
  53. {
  54.     return &x_xfont_procs;
  55. }
  56.  
  57. /* Define a X11 xfont. */
  58. typedef struct x_xfont_s x_xfont;
  59.   struct x_xfont_s {
  60.       gx_xfont_common common;
  61.       gx_device_X *xdev;
  62.       XFontStruct *font;
  63.       int encoding_index;
  64.       int My;
  65.       int angle;
  66.   };
  67.  
  68. /* Look up a font. */
  69. private gx_xfont *
  70. x_lookup_font(gx_device *dev, const byte *fname, uint len,
  71.           int encoding_index, const gs_uid *puid, const gs_matrix *pmat,
  72.           const gs_memory_procs *mprocs)
  73. {
  74.     gx_device_X *xdev = (gx_device_X *) dev;
  75.     x_xfont *xxf;
  76.     char x11template[256];
  77.     char *x11fontname;
  78.     XFontStruct *x11font;
  79.     x11fontmap *fmp;
  80.     int i;
  81.     double diff, d;
  82.     double height;
  83.     int size;
  84.     int xwidth, xheight, angle;
  85.     Boolean My;
  86.     Boolean scalable_font;
  87.  
  88.     if (!xdev->useXFonts) return NULL;
  89.  
  90.     if (pmat->xy == 0 && pmat->yx == 0) {
  91.     xwidth  = fabs(pmat->xx * 1000) + 0.5;
  92.     xheight = fabs(pmat->yy * 1000) + 0.5;
  93.     height  = fabs(pmat->yy * 1000);
  94.     if (pmat->xx > 0) angle = 0;
  95.     else angle = 180;
  96.     My = pmat->xx > 0 && pmat->yy > 0 || pmat->xx < 0 && pmat->yy < 0;
  97.     } else if (pmat->xx == 0 && pmat->yy == 0) {
  98.     xwidth  = fabs(pmat->xy * 1000) + 0.5;
  99.     xheight = fabs(pmat->yx * 1000) + 0.5;
  100.     height  = fabs(pmat->yx * 1000);
  101.     if (pmat->yx < 0) angle = 90;
  102.     else angle = 270;
  103.     My = pmat->yx > 0 && pmat->xy < 0 || pmat->yx < 0 && pmat->xy > 0;
  104.     } else {
  105.     return NULL;
  106.     }
  107.  
  108.     if (encoding_index == 0 || encoding_index == 1) {
  109.     int tried_other_encoding = 0;
  110.  
  111.     fmp = xdev->regular_fonts;
  112.     while (fmp) {
  113.         if (len == strlen(fmp->ps_name) &&
  114.         strncmp(fmp->ps_name, (char *)fname, len) == 0) break;
  115.         fmp = fmp->next;
  116.     }
  117.     if (fmp == NULL) return NULL;
  118.     while (True) {
  119.         if (encoding_index == 0) {
  120.         scalable_font = False;
  121.         if (fmp->std_count == -1) {
  122.             sprintf(x11template, "%s%s", fmp->x11_name,
  123.                 "-*-*-*-*-*-*-Adobe-fontspecific");
  124.             fmp->std_names = XListFonts(xdev->dpy, x11template, 32,
  125.                             &fmp->std_count);
  126.         }
  127.         if (fmp->std_count) {
  128.             diff = 999.9;
  129.             for (i = 0; i < fmp->std_count; i++) {
  130.             char *szp = fmp->std_names[i] + strlen(fmp->x11_name)+1;
  131.  
  132.             size = 0;
  133.             while (*szp >= '0' && *szp <= '9')
  134.                 size = size * 10 + *szp++ - '0';
  135.             if (size == 0) {
  136.                 scalable_font = True;
  137.                 continue;
  138.             }
  139.             d = fabs(height - size) / height;
  140.             if (d < diff) {
  141.                 x11fontname = fmp->std_names[i];
  142.                 diff = d;
  143.             }
  144.             }
  145.             if (xdev->useScalableFonts && scalable_font) {
  146.             d = fabs(height - xheight) / height;
  147.             if (d < diff) {
  148.                 sprintf(x11template, "%s-%d%s", fmp->x11_name,
  149.                     xheight, "-0-0-0-*-0-Adobe-fontspecific");
  150.                 x11fontname = x11template;
  151.                 diff = 0;    /* Always use scalable font if closer */
  152.             }
  153.             }
  154.             if (diff*100 <= xdev->xFontTolerance) break;
  155.         }
  156.         if (tried_other_encoding) return NULL;
  157.         encoding_index = 1;
  158.         tried_other_encoding = 1;
  159.         } else if (encoding_index == 1) {
  160.         scalable_font = False;
  161.         if (fmp->iso_count == -1) {
  162.             sprintf(x11template, "%s%s", fmp->x11_name,
  163.                 "-*-*-*-*-*-*-ISO8859-1");
  164.             fmp->iso_names = XListFonts(xdev->dpy, x11template, 32,
  165.                             &fmp->iso_count);
  166.         }
  167.         if (fmp->iso_count) {
  168.             diff = 999.9;
  169.             for (i = 0; i < fmp->iso_count; i++) {
  170.             char *szp = fmp->iso_names[i] + strlen(fmp->x11_name)+1;
  171.  
  172.             size = 0;
  173.             while (*szp >= '0' && *szp <= '9')
  174.                 size = size * 10 + *szp++ - '0';
  175.             if (size == 0) {
  176.                 scalable_font = True;
  177.                 continue;
  178.             }
  179.             d = fabs(height - size) / height;
  180.             if (d < diff) {
  181.                 x11fontname = fmp->iso_names[i];
  182.                 diff = d;
  183.             }
  184.             }
  185.             if (xdev->useScalableFonts && scalable_font) {
  186.             d = fabs(height - xheight) / height;
  187.             if (d < diff) {
  188.                 sprintf(x11template, "%s-%d%s", fmp->x11_name,
  189.                     xheight, "-0-0-0-*-0-ISO8859-1");
  190.                 x11fontname = x11template;
  191.                 diff = 0;    /* Always use scalable font if closer */
  192.             }
  193.             }
  194.             if (diff*100 <= xdev->xFontTolerance) break;
  195.         }
  196.         if (tried_other_encoding) return NULL;
  197.         encoding_index = 0;
  198.         tried_other_encoding = 1;
  199.         }
  200.     }
  201.     } else if (encoding_index == 2 || encoding_index == 3) {
  202.     scalable_font = False;
  203.     if (encoding_index == 2) fmp = xdev->symbol_fonts;
  204.     if (encoding_index == 3) fmp = xdev->dingbat_fonts;
  205.     while (fmp) {
  206.         if (len == strlen(fmp->ps_name) &&
  207.         strncmp(fmp->ps_name, (char *)fname, len) == 0)
  208.         break;
  209.         fmp = fmp->next;
  210.     }
  211.     if (fmp == NULL) return NULL;
  212.     if (fmp->std_count == -1) {
  213.         sprintf(x11template, "%s%s", fmp->x11_name,
  214.             "-*-*-*-*-*-*-Adobe-fontspecific");
  215.         fmp->std_names = XListFonts(xdev->dpy, x11template, 32,
  216.                         &fmp->std_count);
  217.     }
  218.     if (fmp->std_count) {
  219.         diff = 999.9;
  220.         for (i = 0; i < fmp->std_count; i++) {
  221.         char *szp = fmp->std_names[i] + strlen(fmp->x11_name)+1;
  222.  
  223.         size = 0;
  224.         while (*szp >= '0' && *szp <= '9')
  225.             size = size * 10 + *szp++ - '0';
  226.         if (size == 0) {
  227.             scalable_font = True;
  228.             continue;
  229.         }
  230.         d = fabs(height - size) / height;
  231.         if (d < diff) {
  232.             x11fontname = fmp->std_names[i];
  233.             diff = d;
  234.         }
  235.         }
  236.         if (xdev->useScalableFonts && scalable_font) {
  237.         d = fabs(height - xheight) / height;
  238.         if (d < diff) {
  239.             sprintf(x11template, "%s-%d%s", fmp->x11_name, xheight,
  240.                 "-0-0-0-*-0-Adobe-fontspecific");
  241.             x11fontname = x11template;
  242.             diff = 0;    /* Always use scalable font if closer */
  243.         }
  244.         }
  245.         if (diff*100 > xdev->xFontTolerance) return NULL;
  246.     } else {
  247.         return NULL;
  248.     }
  249.     } else {
  250.     return NULL;
  251.     }
  252.  
  253.     if (xwidth != xheight || angle != 0 || My) {
  254.     if (!xdev->useScalableFonts || !scalable_font) return NULL;
  255.     sprintf(x11template, "%s%s+%d-%d+%d%s",
  256.         fmp->x11_name, My ? "+My" : "",
  257.         angle*64 , xheight, xwidth,
  258.         encoding_index == 1 ? "-0-0-0-*-0-ISO8859-1" :
  259.         "-0-0-0-*-0-Adobe-fontspecific");
  260.     x11fontname = x11template;
  261.     }
  262.  
  263.     x11font = XLoadQueryFont(xdev->dpy, x11fontname);
  264.     if (x11font == NULL)
  265.     return NULL;
  266.     /* Don't bother with 16-bit or 2 byte fonts yet */
  267.     if (x11font->min_byte1 || x11font->max_byte1) {
  268.     XFreeFont(xdev->dpy, x11font);
  269.     return NULL;
  270.     }
  271.     xxf = (x_xfont *) (*mprocs->alloc) (1, sizeof(x_xfont), "x_lookup_font");
  272.     if (xxf == NULL)
  273.     return NULL;
  274.     xxf->common.p